/*
 * Jenny Ruppen 09-730-391
 * Sebastien Goldberg 08-914-855
 * Henry Raymond 08-928-061
 * Stefan Mussato 09-752-270
 */
 
package ch.uzh.ifi.sopra.gruppe9.game;

import com.jme3.font.BitmapFont;
import com.jme3.font.BitmapText;
import com.jme3.scene.Node;


/**
 * Prints a text in the GUI.
 */
public class GuiText extends Node {
	
	
	/**
	 * Text to display. Also used as jME3 node name.
	 */
	private String text;
	
	
	/**
	 * Offset of the coordinate system of this text.
	 */
	private GUI.Offset offset;
	
	
	/**
	 * Position of the text in the x axis. (Assuming the window has a width of 1024 pixel.)
	 */
	private int positionX;
	
	
	/**
	 * Position of the text in the y axis. (Assuming the window has a height of 768 pixel.)
	 */
	private int positionY;
	
	
	/**
	 * Z-Index of the element. An element with a higher index will cover an element with a lower one.
	 */
	private int zIndex;
	
	
	/**
	 * Geometry of the text.
	 */
	private BitmapText bitmapText;
	
	
	/**
	 * Font used for the text.
	 */
	private BitmapFont font;
	
	
	/**
	 * Constructor without setting font. Basic font will be used.
	 */
	public GuiText(String text) {
		this.text = text;
		this.font = Fonts.getNormal();
		buildText();
	}
	
	
	/**
	 * Constructor with font.
	 */
	public GuiText(String text, BitmapFont font) {
		this.text = text;
		this.font = font;
		buildText();
	}
	
	
	/**
	 * Creates Text with given font.
	 */
	private void buildText() {
		if(bitmapText != null && this.hasChild(bitmapText)) {
			this.detachChild(bitmapText);
		}
		bitmapText = new BitmapText(font, false);
		bitmapText.setSize(GUI.getFontSize());
		bitmapText.setText(this.text);
		this.attachChild(bitmapText);
	}
	
	
	/**
	 * Set text and create new bitmap text geometry.
	 */
	public void setText(String text) {
		this.text = text;
		buildText();
		bitmapText.setLocalTranslation(GUI.calculateX(offset, positionX), GUI.calculateY(offset, positionY), (float)zIndex);
	}
	
	
	/**
	 * Sets position and size
	 */
	public void setPositionSize(GUI.Offset offset, int positionX, int positionY, int zIndex) {
		this.offset = offset;
		this.positionX = positionX;
		this.positionY = positionY;
		this.zIndex = zIndex;
		bitmapText.setLocalTranslation(GUI.calculateX(offset, positionX), GUI.calculateY(offset, positionY), (float)zIndex);
	}
	
	
	/**
	 * Get line width of bitmap text.
	 */
	public float getLineWidth() {
		return bitmapText.getLineWidth() / GUI.getPixelScale();
	}
	
	
	/**
	 * Get line height of bitmap text.
	 */
	public float getLineHeight() {
		return bitmapText.getLineHeight() / GUI.getPixelScale();
	}
	
	
	/**
	 * Get width of given text.
	 */
	public static int getTextWidth(String text, BitmapFont font) {
		GuiText textGeometry = new GuiText(text, font);
		return (int)textGeometry.getLineWidth();
	}
	
	
	/**
	 * Get string cut off to given width.
	 */
	public static String getCroppedText(String text, BitmapFont font, int maxWidth) {
		String cropped = "";
		for(int i = 1; i <= text.length(); i++) {
			if(getTextWidth(text.substring(0, i), font) > maxWidth) {
				break;
			}
			cropped = text.substring(0, i);
		}
		return cropped;
	}
}
